package com.xfcy.blog.controller.web;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.xfcy.blog.annotation.BusinessLogger;
import com.xfcy.blog.common.R;
import com.xfcy.blog.dto.EmailLoginDTO;
import com.xfcy.blog.dto.EmailRegisterDTO;
import com.xfcy.blog.dto.UserAuthDTO;
import com.xfcy.blog.service.UserAuthService;
import com.xfcy.blog.service.UserService;
import com.xfcy.blog.valid.AddGroup;
import com.xfcy.blog.valid.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 晓风残月Lx
 * @date 2023/4/19 17:39
 */
@RequestMapping("/user")
@RestController
@Api(tags = "登录接口")
public class WebUserController {

    @Resource
    private UserAuthService userAuthService;

    @BusinessLogger(value = "邮箱账号登录",type = "修改",desc = "邮箱账号登录")
    @RequestMapping(value = "/sendEmailCode", method = RequestMethod.GET)
    @ApiOperation(value = "邮箱发送验证码", httpMethod = "POST", response = R.class, notes = "邮箱发送验证码")
    public R sendEmailCode(String email) {
        return userAuthService.sendEmailCode(email);
    }

    @RequestMapping(value = "/emailRegister",method = RequestMethod.POST)
    @ApiOperation(value = "邮箱账号注册", httpMethod = "POST", response = R.class, notes = "邮箱账号注册")
    public R emailRegister(@Validated(AddGroup.class) @RequestBody EmailRegisterDTO emailRegisterDTO){
        return userAuthService.emailRegister(emailRegisterDTO);
    }

    @BusinessLogger(value = "邮箱账号登录",type = "修改",desc = "邮箱账号登录")
    @RequestMapping(value = "/emailLogin", method = RequestMethod.POST)
    @ApiOperation(value = "邮箱账号登录", httpMethod = "POST", response = R.class, notes = "邮箱账号登录")
    public R emailLogin(@Validated(AddGroup.class) @RequestBody EmailLoginDTO emailLoginDTO) {
        return userAuthService.emailLogin(emailLoginDTO);
    }

    @GetMapping("/logout")
    @ApiOperation(value = "注销", httpMethod = "GET", response = R.class, notes = "注销")
    public R logout() {
        return userAuthService.logout();
    }

    @BusinessLogger(value = "个人中心模块-更新个人信息",type = "修改",desc = "更新个人信息")
    @PostMapping("/updateUser")
    @ApiOperation(value = "个人中心模块-更新个人信息", httpMethod = "POST", response = R.class, notes = "更新个人信息")
    @SaCheckLogin
    public R updateUser(@Validated(UpdateGroup.class) @RequestBody UserAuthDTO userAuthDTO) {
        return userAuthService.updateUser(userAuthDTO);
    }

    @RequestMapping(value = "/bindEmail",method = RequestMethod.POST)
    @SaCheckLogin
    @ApiOperation(value = "个人中心模块-绑定邮箱", httpMethod = "POST", response = R.class, notes = "绑定邮箱")
    @BusinessLogger(value = "个人中心模块-绑定邮箱",type = "修改",desc = "绑定邮箱")
    public R bindEmail(@RequestBody EmailLoginDTO emailLoginDTO){
        return userAuthService.bindEmail(emailLoginDTO);
    }

    @BusinessLogger(value = "个人中心模块-邮箱账号修改密码", type = "修改", desc = "邮箱账号修改密码")
    @PostMapping(value = "/updatePassword")
    @ApiOperation(value = "个人中心模块-邮箱账号修改密码", httpMethod = "POST", response = R.class, notes = "邮箱账号修改密码")
    public R updatePwd(@Validated(UpdateGroup.class) @RequestBody EmailRegisterDTO emailRegisterDTO) {
        return userAuthService.updatePassword(emailRegisterDTO);
    }
}
